﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace RunUOGenerator
{
    public partial class ItemTypeControl : BaseTabControl
    {
        //public delegate void EventHandler(object sender, EventArgs e);
        public event EventHandler ItemChanged;

        private XmlDocument doc = new XmlDocument();

        private string docPath = "";
        private UOItem _UOItem = new UOItem();

        public ItemTypeControl()
        {
            InitializeComponent();
        }

        public string DocumentPath { set { docPath = value; } }
        public UOItem UOItem { get { return _UOItem; } }

        public void LoadDocument()
        {
            if (docPath != "")
            {
                try
                {
                    doc.Load(docPath);

                    foreach (XmlNode category in doc["Categories"].ChildNodes)
                    {
                        comboBoxCategory.Items.Add(category.Attributes["Name"].Value);
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Error loading the specified file: " + docPath, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            this.Width = comboBoxCategory.Width;
        }

        private void comboBoxCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBoxItem.Items.Clear();

            foreach (XmlNode category in doc["Categories"].ChildNodes)
            {
                if (category.Attributes["Name"].Value == comboBoxCategory.SelectedItem.ToString())
                {
                    foreach (XmlNode item in category["Items"].ChildNodes)
                    {
                        comboBoxItem.Items.Add(item.Attributes["Name"].Value);
                    }
                }
            }
        }

        private void comboBoxItem_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (XmlNode category in doc["Categories"].ChildNodes)
            {
                if (category.Attributes["Name"].Value == comboBoxCategory.SelectedItem.ToString())
                {
                    foreach (XmlNode item in category["Items"].ChildNodes)
                    {
                        if (item.Attributes["Name"].Value == comboBoxItem.SelectedItem.ToString())
                        {
                            UOItem uoItem = new UOItem();
                            uoItem.Base = item.Attributes["Base"].Value;
                            uoItem.GumpID = item.ChildNodes[1].InnerText; // Gump
                            uoItem.ItemID = item.ChildNodes[0].InnerText; // Item

                            _UOItem = uoItem;

                            base.Value = comboBoxItem.SelectedItem.ToString();
                        }
                    }
                }
            }

            ItemChanged(this, e);
        }
    }
}
